<?php


namespace app\api\service;


use app\api\model\SubjectAnalog;
use app\api\model\SubjectAnalogError;
use app\api\model\SubjectCourse;
use app\api\model\SubjectType;
use think\Collection;

class Test
{
    /**
     * 获取考试题
     * @param $course
     * @param $car_status
     * @return false|mixed|\PDOStatement|string|Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
        public function getExam($uid,$course,$car_status){
//            $result = Redis::getInstance()->get(50,'subject:course:'.$course.':car_status:'.$car_status.':uid:'.$uid);
//            if (!empty($result)){
//                return $result;
//            }
            if ($car_status == 1){//小车
                $condition = [1];//小车分类
                $result = SubjectType::getTypeList($course,$condition);
            }elseif($car_status == 2){//货车
                $condition = [1,2];
                $result = SubjectType::getTypeList($course,$condition);
            }elseif ($car_status == 3){//客车
                $condition = [1,3];
                $result = SubjectType::getTypeList($course,$condition);
            }
            $ids = $result->column('id');
            if ($course == 1){
                $judge= SubjectCourse::getJudgeExamSubject($ids);//获取查询的判断题目
                $choose= SubjectCourse::getChooseExamSubject($ids);//获取查询的选择题目
                list($result1, $list) = $this->getCourseInfo($judge, $list=[],40);//判断题查询
                list($result2, $list) = $this->getCourseInfo($choose, $list=[],60);//选择题查询
                $result = array_merge($result1,$result2);//科目一题目
            }elseif ($course == 4){
                $judge= SubjectCourse::getJudgeExamSubject($ids);//获取查询的判断题目
                $choose= SubjectCourse::getChooseExamSubject($ids);//获取查询的选择题目
                $multiple = SubjectCourse::getChooseExamSubject($ids,1);//获取多选题
                list($result1, $list) = $this->getCourseInfo($judge, $list=[],22);//查询判断数据
                list($result2, $list) = $this->getCourseInfo($choose, $list=[],18);//查询选择数据
                list($result3, $list) = $this->getCourseInfo($multiple, $list=[],10);//查询多选数据
                $result = array_merge($result1,$result2,$result3);//科目4考题
            }
//            Redis::getInstance()->set(50,'subject:course:'.$course.':car_status:'.$car_status.':uid:'.$uid,$result,45*60);//存入缓存
            return $result;
        }

    /**
     * 添加考试记录
     * @param $uid
     * @param $data
     * @return false|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
        public function insertLog($uid,$t_id,$data){
            $analog['user_id'] = $uid;
            $analog['course'] = $data['course'];
            $analog['car_status'] = $data['car_status'];
            $analog['record'] = $data['record'];
            $analog['time'] = $data['time'];
            $analog['t_id'] =$t_id;
            $log = new SubjectAnalog();
            $result = $log->insertLog($analog);//添加考试记录
            $error = $this->getError($uid,$data['course'],$data['car_status']);//查询用户原有错题
            $ids = $error->column('c_id');
            $diff = array_diff($data['error'],$ids);
            if(!empty($diff)){
               $result = $this->insertError($uid,$data['course'],$result,$data['car_status'],$data['error']);
            }
            return $result;
        }

    /**
     * 添加模拟错题
     * @param $uid
     * @param $course
     * @param $analog_id
     * @param $car_status
     * @param $data
     * @return false|int
     */
        public function insertError($uid,$course,$analog_id,$car_status,$data){
            $info = [];
            foreach ($data as $key=>$item){
                $info[$key]['user_id'] =$uid;
                $info[$key]['course'] = $course;
                $info[$key]['c_id'] = $item;
                $info[$key]['analog_id'] = $analog_id;
                $info[$key]['car_status'] = $car_status;
            }
            $error = new SubjectAnalogError();
            $result = $error->addError($info);//添加模拟错题
            return $result;
        }

    /**
     * 获取用户模拟错题
     * @param $uid
     * @param $course
     * @param $car_status
     * @return false|\PDOStatement|string|Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
        public function getError($uid,$course,$car_status){
            $error = new SubjectAnalogError();
            $result = $error->getAnalogError($uid,$course,$car_status);
            foreach ($result as $item){
                $item->visible(['user_id','false_id','subject']);
            }
            return $result;
        }

    /**
     * 删除用户错题
     * @param $false_id
     * @return int
     */
        public function deleteError($false_id){
            $error = new SubjectAnalogError();
            $result =  $error->deleteError($false_id);
            return $result;
        }

    /**
     * 获取用户考试记录
     * @param $uid
     * @param $course
     * @return false|\PDOStatement|string|Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
        public function getUserLog($uid,$course,$car_status){
            $log = new SubjectAnalog();
            $result = $log->getLog($uid,$course,$car_status);
            return $result;
        }

    /**
     * 获取用户最好成绩
     * @param $uid
     * @param $course
     * @param $car_status
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
        public function getUserMaxRecode($uid,$course,$car_status){
            $log = new SubjectAnalog();
            $result = $log->getUserLog($uid,$course,$car_status);
            return $result;
        }

    /**
     * 获取用户排行
     * @param $uid
     * @param $t_id
     * @param $course
     * @param $car_status
     * @return false|\PDOStatement|string|Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
        public function getUserRank($uid,$t_id,$course,$car_status){
            $log = new SubjectAnalog();
            $result =  $log->getLogByTid($t_id,$course,$car_status);
            $max = $this->getUserMaxRecode($uid,$course,$course);
            $recod = $max->record;
            $list =  $result->column('record');
            $tag = array_search($recod,$list);
            $result = $result->toArray();
            if ($tag !== false){
                $data['rank'] = $result;
                $data['user']['rank'] = $tag+1;
                $data['user']['record'] = $recod;
                $data['user']['time'] = $max->time;
                $data['user']['user'] = $max->user;
            }else{
                $data['rank'] = $result;
                $data['user']['rank'] = "";
                $data['user']['record'] = $recod;
                $data['user']['time'] = $max->time;
                $data['user']['user'] = $max->user;
            }
            return $data;
        }

    /**
     * 获取安全视频
     * @return mixed
     */
        public function getVideoInfo(){
          $info = config('video.video_info');
          return $info;
        }

    /**
     *获取题目练习
     * @param $result
     * @param array $list
     * @return array
     */
    private function getCourseInfo(Collection $result, array $list, int $num): array
    {
        $result = $result->shuffle();//数据打乱
        $result = $result->slice(0, $num)->toArray();//取出对应的练习题
        foreach ($result as $key => $item) {
            $list[] = $item['item1'];
            $list[] = $item['item2'];
            if (!empty($item['item3'])) {
                $list[] = $item['item3'];
            }
            if (!empty($item['item4'])) {
                $list[] = $item['item4'];
            }
            shuffle($list);
            $result[$key]['option'] = $list;
            $result[$key]['result'] = '';//选择结果
            unset($result[$key]['item1']);
            unset($result[$key]['item2']);
            unset($result[$key]['item3']);
            unset($result[$key]['item4']);
            $list = [];
        }
        return array($result, $list);
    }





}